eval 'exec perl -wS $0 ${1+"$@"}'
 if 0;

use strict;
use warnings;
use POSIX qw(strftime);

use DBI();
use File::Basename;
use File::stat;
use Data::Dumper 'Dumper';

use IO::Socket;

sub print_h_content
{
    my $hash=shift @_;
    
    foreach my $thekey (keys %$hash)
    {
	print $thekey." => ".$hash->{$thekey}."\n";
    }
}

sub slsecd_connect
{
    my $socket=shift @_;
    my $proto=shift @_;
    my $peeraddr=shift @_;
    my $peerport=shift @_;
    
    $$socket=IO::Socket::INET->new( Proto     => $$proto,
				      PeerAddr  => $$peeraddr,
				      PeerPort  => $$peerport
				      );
    unless ($$socket)
    {
	return 0;
    }
    return 1;
}

sub slsecd_disconnect
{
    my $socket=shift @_;
    
    close $$socket;
}

sub get_enciphered_data
{
    my $h_ref_deciphered_keys = shift @_;
    my $res;
    my $socket=0;
    my $proto="tcp";
    #my $peeraddr="172.24.61.137";
    my $peeraddr="127.0.0.1";
    my $peerport="30000";

    if (! slsecd_connect(\$socket,\$proto,\$peeraddr,\$peerport))
    {
	error("Failed to connect to slsecd.\n");
	return 4;
    }
    
    foreach my $thekey (keys %$h_ref_deciphered_keys)
    {
	#print $thekey;
	if ($h_ref_deciphered_keys->{$thekey}=~/KO/)
	{
	    #chop ($thekey);
	    print "$thekey;KO;\n";
	    #push(@$ref_deciphered_keys,"$thekey;KO");
	}
	else
	{
	    #print { $h_ref_param->{SOCKET} } "D;" .$thekey. ";" .$h_ref_enciphered_keys->{$thekey}. "\n";
	    print $socket "E;" .$thekey. ";" .$h_ref_deciphered_keys->{$thekey}. "\n";

	    $res = <$socket>;
	    print $res;
	    #chomp($res);
	    #push(@$ref_deciphered_keys,$res);
	}
    }

    #print { $h_ref_param->{SOCKET} } "Q\n";
    print $socket "Q\n";
    if (! slsecd_disconnect(\$socket))
    {
	warning("Failed to disconnect from slsecd.\n");
    }
    
    return 1;
}

#my $myhash=myhash();
#print $myhash."\n"
#get_deciphered_data(%hash);

my %decipherd_list=();

%decipherd_list = (
		   '354900011415836' => '4983884450;;5298814243;;;;',
		   '353205013165582' => '2484667553;;;;;2677259792;',
		   '353205011959846' => '3068503321;;;;;2853606627;',
		   '353205012227409' => '1681849264;;;;;4130250726;',
		   '353205014956724' => '1983918327;;;;;0979988888;'
		   );

#print_h_content(\%hash);
get_enciphered_data(\%decipherd_list);
